package com.gridmobi.xrtb.conf;


import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.util.Properties;

public class SSHConnection {

    private final static String S_PATH_FILE_PRIVATE_KEY = "/home/ec-user/.ssh/aaaaa";
    private final static String S_PATH_FILE_KNOWN_HOSTS = "/home/ec-user/.ssh/known_hosts";
    private final static String S_PASS_PHRASE = "";
    private final static int LOCAl_PORT = 3307;
    private final static int REMOTE_PORT = 3306;
    private final static int SSH_REMOTE_PORT = 1022;
    private final static String SSH_USER = "ec-user";
    private final static String SSH_REMOTE_SERVER = "44.235.122.213";
    private final static String MYSQL_REMOTE_SERVER = "44.235.122.213";

    private Session sesion; //represents each ssh session

    public void closeSSH() {
        sesion.disconnect();
    }

    public SSHConnection() throws Throwable {

        JSch jsch = null;

        jsch = new JSch();
        jsch.setKnownHosts(S_PATH_FILE_KNOWN_HOSTS);
        //jsch.addIdentity(S_PATH_FILE_PRIVATE_KEY);

        sesion = jsch.getSession(SSH_USER, SSH_REMOTE_SERVER, SSH_REMOTE_PORT);


        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        sesion.setConfig(config);

        sesion.connect(); //ssh connection established!

        //by security policy, you must connect through a fowarded port
        sesion.setPortForwardingL(LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT);

    }
}
